
package com.menhai.personmanagement.services;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.menhai.personmanagement.entity.Codelist;
import com.menhai.personmanagement.entity.EducationLog;
import com.menhai.personmanagement.mapper.CodelistMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务类
 * </p>
 *
 * @author shikun
 * @since 2021-11-24
 */
@Service
public class ICodelistService {

    @Autowired
    private CodelistMapper codelistMapper;

    /**
     * @param kindCode
     * @describe
     * @Return java.util.List<com.menhai.personmanagement.entity.Codelist>
     * @Date 2021/11/25 8:57
     * @author shikun
     */
    public List<Codelist> selectByCodeKindCode(String kindCode) {

        return codelistMapper.selectByCodeKindCode(kindCode);

    }

    public List<Codelist> findOneType(String type){
        return codelistMapper.findOneType(type);
    }

    /**
     * @describe 医院下级
     * @param codeValue
     * @Return java.util.List<com.menhai.personmanagement.entity.Codelist>
     * @Date 2021/12/16 10:55
     * @author shikun
     */
    public List<Codelist> getLowerlevel(String codeValue) {
        return codelistMapper.getLowerlevel(codeValue);
    }


    public List<Codelist> getHospital() {
        return codelistMapper.getHospital();
    }

    public List<Codelist> getHospitalTwo(String code) {
        return codelistMapper.getHospitalTwo(code);
    }

    public List<Codelist> getHospitalThree(String code) {
        return codelistMapper.getHospitalThree(code);
    }


    public List<Codelist> getZhenHospital() {
        return codelistMapper.getZhenHospital();
    }

    public List<Codelist> getChunHospital() {
        return codelistMapper.getChunHospital();
    }
    
    public List<Codelist> getChronicList(){
    	return codelistMapper.getChronicList();
    }
    
    public List<Codelist> getKindCodeList(){
    	return codelistMapper.getKindCodeList();
    }
    
    /**
     * 获取字典列表
     * @param codelist
     * @param kindCode
     * @return
     */
	public IPage<Codelist> getCodeList(Codelist codelist){
		Page<Codelist> page = new Page<>(codelist.getPageIndex(),codelist.getPageSize());
		return codelistMapper.getCodeList(page,codelist);
	}


	@Transactional
	public int updateCodeAll(Codelist codelist) {
		return codelistMapper.updateUpnameById(codelist);
	}

	@Transactional
	public int deleteOwnthefollowing(Codelist codelist) {

		List codelist1 = codelistMapper.codelistBycodelistId(codelist.getCodeValue());

		List populationBycodelistId = codelistMapper.populationBycodelistId(codelist.getCodeValue());
		if(codelist1.size()>0 || populationBycodelistId.size()>0){
			return 0;
		}else{
			List<Codelist> codelistList = codelistMapper.selectlikecodeValue(codelist.getCodeValue());
			List<Integer> codelistids = codelistList.stream().map(Codelist::getCodelistId).collect(Collectors.toList());
			return 	codelistMapper.deleteBatchIds(codelistids);
		}
	}

	@Transactional
	public int adddateCodelowerlevel(Codelist codelist) {

		codelist.setKindCode("YYMC");
		codelist.setKindName("地域划分");
		codelist.setIsShow("0");
		codelist.setMemo("");
		codelist.setCodeName(codelist.getCodeName());
		codelist.setCodelistId(null);
		Codelist codelist1 = null;

		boolean isnotempty = true;

		//判断 codevalue 是几位
		if(codelist.getCodeValue().length()==4){
			codelist1 =  codelistMapper.selectOrdershort(codelist.getCodeValue());
			if(codelist1==null){//判断是否为空
				isnotempty=false;
			}
		}else{
			codelist1 =  codelistMapper.selectOrder(codelist.getCodeValue());
			if(codelist1==null){
				isnotempty=false;
			}
		}
		if(isnotempty){
			codelist.setOrderNum(Integer.valueOf(codelist1.getOrderNum())+1+"");
			int strh = Integer.valueOf(codelist1.getCodeValue().substring(codelist1.getCodeValue().length() -2,codelist1.getCodeValue().length()));   //截取
			String  plusone = "";
			if(strh<9){
				plusone = "0"+(strh+1);
			}else{
				plusone = (strh+1)+"";
			}
			String strm = codelist1.getCodeValue().substring(0,codelist1.getCodeValue().length()-2)+plusone;
			codelist.setCodeValue(strm);
		}else{
			codelist.setCodeValue(codelist.getCodeValue()+"01");
			codelist.setOrderNum("1");
		}
		return  codelistMapper.insert(codelist);
	}

   /**
  	 * 新增字典
  	 * @param codelist
  	 * @return Map<String, Object>
  	 */
  	@Transactional
  	public Map<String, Object> addCode(Codelist codelist) {
  		Map<String,Object> resultMap = new HashMap<String,Object>();
  		try{
  			int iRet;
  			//查询字典编码是否重复
  			int countBykind = codelistMapper.getCountBykind(codelist.getKindCode(), codelist.getCodeValue());
  			if(countBykind>0){//有重复
  				resultMap.put("success", "0");
  				resultMap.put("message", "新建字典失败，字典编码重复！");
  				return resultMap;
  			}else{//无重复，执行插入
  				codelist.setIsShow("1");
  				iRet = codelistMapper.insert(codelist);
  			} 			
  			if(iRet == 0){  //没有插入成功的数据
  				resultMap.put("success", "0");
  				resultMap.put("message", "插入成功的条数为0，操作字典失败！");
  				return resultMap;
  			}else{
  				resultMap.put("success", "1");
  				resultMap.put("message", "操作字典成功！");
  				return resultMap;
  			}
  		}catch(Exception e){
  			resultMap.put("success", "0");
  			resultMap.put("message", "操作字典失败！异常：" +e.getMessage());
  			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  		}
  		return resultMap;
  	}
  	
  	/**
  	 * 删除字典
  	 * @param codelistId
  	 * @return
  	 */
	@Transactional
	public Map<String, Object> delete(String codelistId) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{	
			//删除字典
			int iRet = codelistMapper.delete(codelistId);
			if(iRet == 0){  
				resultMap.put("success", "0");
				resultMap.put("message", "删除成功的条数为0，删除字典失败！");
				return resultMap;
			}
			resultMap.put("success", "1");
			resultMap.put("message", "删除字典成功！");
		}catch(Exception e){
			resultMap.put("success", "0");
			resultMap.put("message", "删除字典失败！异常：" +e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resultMap;
	}
	
	/**
	 * 编辑字典
	 * @param codelist
	 * @return
	 */
	@Transactional
	public Map<String, Object> updateCode(Codelist codelist) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{		
			int iRet = codelistMapper.update(codelist);	
			if(iRet == 0){  
				resultMap.put("success", "0");
				resultMap.put("message", "编辑成功的条数为0，编辑字典失败！");
				return resultMap;
			}
			resultMap.put("success", "1");
			resultMap.put("message", "编辑字典成功！");
		}catch(Exception e){
			resultMap.put("success", "0");
			resultMap.put("message", "编辑字典失败！异常：" +e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resultMap;
	}
	
	/**
	 * 获取文化程度
	 * @return
	 */
	public List<Codelist> getSTUDYHEIGHT(){
    	return codelistMapper.getSTUDYHEIGHT();
    }
}
